﻿module Sustainalytics.ProductInvolvement.Security {

    export class PIPermissionsService implements IPIPermissionsService {

        private piIndicators : string[] = [
            "ae",
            "al",
            "cw",
            "fa",
            "fl",
            "gb",
            "gm",
            "mc",
            "np",
            "pe",
            "tp",
            "ab",
            "co",
            "sc",
            "pm",
            "pp",
            "at",
            "pl",
            "tc",
            "ac",
            "os"
        ];

        constructor(
            private $q: ng.IQService,
            private ClaimsAuthorizationService: Sustainalytics.Claims.ClaimsAuthorizationService) {
        }

        //util function
        public hasValidPIIndicatorClaim(resName: string, key: string, value: string): boolean {
            var ctx = this;

            var params: Sustainalytics.Claims.ResourceParameters[] = new Array();
            var resPIParam = new Sustainalytics.Claims.ResourceParameters();

            resPIParam.key = key;
            resPIParam.value = value;
            params.push(resPIParam);

            var validPI = ctx.ClaimsAuthorizationService.Validate('api/v1/' + resName , 'GET', params);

            return validPI;
        }

        //Company/PI claim && at least one of the 19 PI claims
        public hasCompanyPI(): boolean {
            var ctx = this;

            var params: Sustainalytics.Claims.ResourceParameters[] = new Array();

            var validCompanyReport = this.hasValidPIIndicatorClaim('companyassessments', 'pi', 'companyreport');

            var atLeastOnePIClaim = false;
            $.each(ctx.piIndicators, (index, indicator) => {
                var claimedInd = this.hasValidPIIndicatorClaim('companyassessments', 'pi', indicator);
                atLeastOnePIClaim = atLeastOnePIClaim || claimedInd;

                if (atLeastOnePIClaim)
                    return false;
            });

            return validCompanyReport && atLeastOnePIClaim;
        }


        //(Research/PI claim && at least one of the 19 PI claims) || Compliance/Sanctions claim
        public hasMultiCompanyPI(): boolean {
            var ctx = this;

            var validMultiCompanyReport = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'multicompanyreport');
            
            var atLeastOnePIClaim = false;
            $.each(ctx.piIndicators,(index, indicator) => {
                var claimedInd = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', indicator);
                atLeastOnePIClaim = atLeastOnePIClaim || claimedInd;

                if (atLeastOnePIClaim)
                    return false;
            });

            var validComplianceSanctionsClaim = this.hasValidPIIndicatorClaim('companyassessmentshortreports', 'pi', 'sanctions');

            return (validMultiCompanyReport && atLeastOnePIClaim) || validComplianceSanctionsClaim;
        }
    }


    angular.module(piSecurityModuleName).service('PIPermissionsService', ['$q', 'ClaimsAuthorizationService', PIPermissionsService]);
}